import { goodsCate } from '@/api/goods'
import { getCategoryData, setCategoryData, removeCategoryData } from '@/utils/auth'
import moment from 'moment'

const state = {
  category: JSON.parse(getCategoryData() || '[]'),
}

const mutations = {
  SET_CATEGORY: (state, data) => {
    const obj = {
      data: data || [],
      time: moment(new Date()).format('YYYY-MM-DD HH:mm')
    };
    state.category = { ...obj };
    setCategoryData(JSON.stringify(obj))
  }
}

const actions = {
  // user login
  getCategoryList({ commit }, data = {}) {
    return new Promise((resolve, reject) => {
      goodsCate(data).then(res => {
        const { data } = res
        const list = data?.list || [];
        commit('SET_CATEGORY', list)
        resolve(list)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // getCategoryList({ commit }, data = {}) {
  //   return new Promise((resolve, reject) => {
  //     goodsCate(data).then(res => {
  //       const { data } = res
  //       const list = data?.list || [];
  //       commit('SET_CATEGORY', list)
  //       resolve(list)
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
